﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter05
{
    public class Code01
    {
        public static void Begin()
        {
            {
                byte destinationVar;
                short sourceVar = 7;
                destinationVar = (byte)sourceVar;
                Console.WriteLine("sourceVar val:  {0}", sourceVar);
                Console.WriteLine("destinationVar val: {0}", destinationVar);
                Console.ReadLine();
            }
            {
                byte destinationVar = 0;
                short sourceVar = 281;
                destinationVar = (byte)sourceVar;
                Console.WriteLine("sourceVar val:  {0}", sourceVar);
                Console.WriteLine("destinationVar val: {0}", destinationVar);
                Console.ReadLine();
                destinationVar = 0;
                sourceVar = 281;
                destinationVar = unchecked((byte)sourceVar);
                Console.WriteLine("sourceVar val:  {0}", sourceVar);
                Console.WriteLine("destinationVar val: {0}", destinationVar);
                Console.ReadLine();
            }
            {
                byte destinationVar = 0;
                short sourceVar = 281;
                try
                {
                    destinationVar = checked((byte)sourceVar);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine("sourceVar val:  {0}", sourceVar);
                Console.WriteLine("destinationVar val: {0}", destinationVar);
                Console.ReadLine();
            }
        }

        public static void BeginEx01()
        {
            short shortReault, shortVal = 4;
            int intergerVal = 67;
            long longResult;
            float floatVal = 10.5F;
            double doubleResult, doubleVal = 99.999D;
            string stringResult, stringVal = "17";
            bool boolVal = true;
            Console.WriteLine("Variable Conversion Examples\n");
            doubleResult = floatVal * shortVal;
            Console.WriteLine("Implicit, -> double: {0} * {1} -> {2}", floatVal, shortVal, doubleResult);
            shortReault = (short)floatVal;
            Console.WriteLine("Explicit, -> short: {0} -> {1}", floatVal, shortReault);
            stringResult = Convert.ToString(boolVal) + Convert.ToString(doubleVal);
            Console.WriteLine("Explicit, -> string: \"{0}\" + \"{1}\" -> {2}", boolVal, doubleVal, stringResult);
            longResult = intergerVal + Convert.ToInt64(stringVal);
            Console.WriteLine("Mixed, -> long: {0} + {1} -> {2}", intergerVal, stringVal, longResult);
            Console.ReadLine();
        }
    }
}
